/*
 * Josh Lindoo
 * Login ID : lind6441
 * CS-102, Winter 2013
 * Programming Assignment 1
 * Database class: Collection of movie and showing objects
 */

public class Database {
	
	// Constants for max number of movie and showing entries
	private final int maxMovies = 100; //length of movies array
	private final int maxShowings = 100; //length of showings array 
	private int movieArraySize = -1; //tracks size of movie array
	private int showingArraySize = -1; //tracks size of showings array
	
	private Movie movies[] = new Movie[maxMovies]; // array of movie objs
	private Showing showings[] = new Showing[maxShowings]; //and showing objs
	
	//Default constructor
	public Database () {
		
	}
	
	/*
	 * Method: searchMovies
	 * Purpose: Searches for movies matching the user's input. If the movie
	 * 			is found, the movie data and all of the showings are printed
	 * Parameters:
	 * 		String movieTitle:	The movie title to search for
	 * Returns: void
	 */
	
	public void searchMovies(String movieTitle) {
		boolean movieMatched = false; //track if movie is found
		boolean showingMatched = false; //track if showings are found
		
		//loop through the movies looking for the first matching title
		for(int index = 0; index <= movieArraySize; index++) {
			String movieAtIndex = movies[index].getTitle();
			
			//if the titles match(ignoring case), print the movie data
			if(movieAtIndex.compareToIgnoreCase(movieTitle) == 0) {
				System.out.println(movies[index].toString());
				movieMatched = true;
				
				//loop through showings to find ones that match movie's title
				for(int pos = 0; pos < showingArraySize + 1; pos++) {
					String showingAtPos = showings[pos].getTitle();
					
					//if a showing is found, print that showing's data
					if(movieAtIndex.compareToIgnoreCase(showingAtPos) == 0) {
						System.out.println(showings[pos].toString());
						showingMatched = true;
					}
				}
			}
		}
		
		if(!movieMatched)
			System.out.println("Movie not found..\n");
		else if (!showingMatched) {
			System.out.println("\tNo showings found..\n");
		}
	}
	
	/*
	 * Methods: addMovie, addShowing
	 * Purpose: Add a movie or showing to the database
	 * Parameters:
	 * 		String[] (movieData/showingData): Contains data for the entry
	 * Returns: void
	 */
	
	public void addMovie(String[] movieData) {
		//add at the last entry +1 index
		movies[movieArraySize + 1] = new Movie(movieData);
		//increment the last entry counter
		movieArraySize++;
	}
	
	public void addShowing(String[] showingData) {
		//add at the last entry +1 index
		showings[showingArraySize + 1] = new Showing(showingData);
		//increment the last entry counter
		showingArraySize++;
	}
	
	/*
	 * Method: setShowingTitles
	 * Purpose: Find the titles of the showings using the unique ID
	 * Parameters:
	 * Returns: void
	 */
	
	public void setShowingTitles() {
		for(int index = 0; index < showingArraySize + 1; index++) {
			//save the ID of the showing for convenience
			int currentShowID = showings[index].getID();
			boolean hasMatch = false; //boolean to stop the loop once match
									  //is found
			int count = -1; //counter for loop
			
			//while we haven't found a match
			while(!hasMatch) { 
				count++;
				
				//if the IDs match.. set the title of the showing
				if (movies[count].getID() == currentShowID) {
					showings[index].setTitle(movies[count].getTitle());
					hasMatch = true; //match was found
				}
			}
		}
	}
	
	/*
	 * Method: printMovies, printShowings
	 * Purpose: print all movie or showing objs in readable format
	 * Parameters:
	 * Returns: void
	 */
	
	public void printMovies () {
		System.out.println(""); // \n
		
		//call the toString method on each movie in db
		for(int index = 0; index < movieArraySize + 1; index++)
			System.out.println(movies[index].toString());
	}
	
	public void printShowings () {
		System.out.println(""); // \n
		
		//call the toString method on each showing in db
		for(int index = 0; index < showingArraySize + 1; index++)
			System.out.println(showings[index].toString());
	}
}
